package com.next.aappublicapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TextView;

import com.google.analytics.tracking.android.EasyTracker;
import com.next.aap.client.DonationSummary;
import com.next.aappublicapp.server.service.DataServices;
import com.next.aappublicapp.tasks.AapAsyncTask;

public class DonationSummaryActivity extends Activity {

	private TableLayout tableLayout;
	private TextView totalDonationTextView;
	private TextView totalDonationYesterdayTextView;
	private TextView todayDonationTextView;
	private TextView yesterdayDonationTextView;
	private TextView maxDonationTextView;
	private TextView yesterdayMaxDonationTextView;
	
	private TextView totalNoOfDonorsTodayTextView;
	private TextView totalNoOfDonorsYesterdayTextView;
	private TextView overallTotalNoOfDonorsTodayTextView;
	private TextView overallTotalNoOfDonorsYesterdayTextView;
	
	private ImageButton donateNoewButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_donation_summary);
		
		totalDonationTextView = (TextView)findViewById(R.id.totalDonationTextView);
		totalDonationYesterdayTextView = (TextView)findViewById(R.id.totalDonationYesterdayTextView);
		todayDonationTextView = (TextView)findViewById(R.id.todayDonationTextView);
		yesterdayDonationTextView = (TextView)findViewById(R.id.yesterdayDonationTextView);
		maxDonationTextView = (TextView)findViewById(R.id.maxDonationTextView);
		yesterdayMaxDonationTextView = (TextView)findViewById(R.id.yesterdayMaxDonationTextView);
		totalNoOfDonorsTodayTextView = (TextView)findViewById(R.id.totalNoOfDonorsTodayTextView);
		totalNoOfDonorsYesterdayTextView = (TextView)findViewById(R.id.totalNoOfDonorsYesterdayTextView);
		overallTotalNoOfDonorsTodayTextView = (TextView)findViewById(R.id.overallTotalNoOfDonorsTodayTextView);
		overallTotalNoOfDonorsYesterdayTextView = (TextView)findViewById(R.id.overallTotalNoOfDonorsYesterdayTextView);
		donateNoewButton = (ImageButton)findViewById(R.id.donateNoewButton);
		loadLatestDonationDetailAysnc();
		
		donateNoewButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(DonationSummaryActivity.this,DonateNowActivity.class);
				DonationSummaryActivity.this.startActivity(intent);
				EasyTracker.getTracker().sendEvent("Android", "Donation", "DonateNow", 1L);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.donation_summary, menu);
		return true;
	}
	
	private void loadDonationSummaryOnScreen(DonationSummary donationSummary){
		if(donationSummary == null){
			Log.i("Ravi", "donationSummary is null");
			return;
		}
		Log.i("Ravi", "adding a table Row");
		totalDonationTextView.setText(donationSummary.getTotalDonation());
		totalDonationYesterdayTextView.setText(donationSummary.getTotalDonationYesterday());
		todayDonationTextView.setText(donationSummary.getTodayDonation());
		yesterdayDonationTextView.setText(donationSummary.getYesterdayDonation());
		maxDonationTextView.setText(donationSummary.getTodayMaxDonation());
		yesterdayMaxDonationTextView.setText(donationSummary.getYesterdayMaxDonation());
		totalNoOfDonorsTodayTextView.setText(donationSummary.getTotalNoOfDonorsToday());
		totalNoOfDonorsYesterdayTextView.setText(donationSummary.getTotalNoOfDonorsYesterday());
		overallTotalNoOfDonorsTodayTextView.setText(donationSummary.getOverallTotalNoOfDonorsToday());
		overallTotalNoOfDonorsYesterdayTextView.setText(donationSummary.getOverallTotalNoOfDonorsYesterday());
		
	}
	
	
	private void loadLatestDonationDetailAysnc(){
		new AapAsyncTask<String, String, DonationSummary>("Donation","getsummary") {
			@Override
			protected DonationSummary doInBackground(String... params) {
				try{
					DonationSummary donationSummary = DataServices.getInstance().getDonationSUmmary();
					return donationSummary;
				}catch(Exception ex){
					ex.printStackTrace();
				}
				return null;
			}
			
			@Override
			protected void afterExecute(DonationSummary donationSummary) {
				loadDonationSummaryOnScreen(donationSummary);
			}
		}.execute();
	}

}
